//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.example.zqathserver.controller;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import java.util.HashMap;
import java.util.Map;

public class EncryptionTest {
    public EncryptionTest() {
    }

    public static void main(String[] args) {
        String fileName = "test.xls";
        Map<String, ComRowSet> mockComRowSetMap = new HashMap();
        writeExcel(fileName, mockComRowSetMap);

        try {
            encryptFile(fileName);
        } catch (Exception var4) {
            Exception e = var4;
            e.printStackTrace();
        }

    }

    public static void writeExcel(String fileName, Map<String, ComRowSet> comRowSetMap) {
        WritableWorkbook workbook = null;

        try {
            workbook = Workbook.createWorkbook(new File(fileName));
            WritableSheet sheet = workbook.createSheet("Mock Data", 0);
            sheet.addCell(new Label(0, 0, "Column 1"));
            sheet.addCell(new Label(1, 0, "Column 2"));
            sheet.addCell(new Label(0, 1, "Data 1"));
            sheet.addCell(new Label(1, 1, "Data 2"));
            workbook.write();
            System.out.println("Excel file created with JXL: " + fileName);
        } catch (Exception var12) {
            Exception e = var12;
            e.printStackTrace();
        } finally {
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (Exception var11) {
                    Exception e = var11;
                    e.printStackTrace();
                }
            }

        }

    }

    public static void encryptFile(String fileName) throws Exception {
        File file = new File(fileName);
        Key secretKey = generateKey();
        processFile(1, secretKey, file);
    }

    private static Key generateKey() throws Exception {
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(128);
        SecretKey secretKey = keyGen.generateKey();
        return new SecretKeySpec(secretKey.getEncoded(), "AES");
    }

    private static void processFile(int cipherMode, Key key, File file) throws Exception {
        File tempFile = new File(file.getAbsolutePath() + ".enc");
        FileInputStream fis = new FileInputStream(file);
        Throwable var5 = null;

        try {
            FileOutputStream fos = new FileOutputStream(tempFile);
            Throwable var7 = null;

            try {
                Cipher cipher = Cipher.getInstance("AES");
                cipher.init(cipherMode, key);
                byte[] inputBytes = new byte[64];

                int bytesRead;
                byte[] outputBytes;
                while((bytesRead = fis.read(inputBytes)) != -1) {
                    outputBytes = cipher.update(inputBytes, 0, bytesRead);
                    if (outputBytes != null) {
                        fos.write(outputBytes);
                    }
                }

                outputBytes = cipher.doFinal();
                if (outputBytes != null) {
                    fos.write(outputBytes);
                }
            } catch (Throwable var33) {
                var7 = var33;
                throw var33;
            } finally {
                if (fos != null) {
                    if (var7 != null) {
                        try {
                            fos.close();
                        } catch (Throwable var32) {
                            var7.addSuppressed(var32);
                        }
                    } else {
                        fos.close();
                    }
                }

            }
        } catch (Throwable var35) {
            var5 = var35;
            throw var35;
        } finally {
            if (fis != null) {
                if (var5 != null) {
                    try {
                        fis.close();
                    } catch (Throwable var31) {
                        var5.addSuppressed(var31);
                    }
                } else {
                    fis.close();
                }
            }

        }

        if (file.delete()) {
            if (!tempFile.renameTo(file)) {
                throw new Exception("Failed to rename temp file to original file");
            } else {
                System.out.println("File encrypted successfully: " + file.getAbsolutePath());
            }
        } else {
            throw new Exception("Failed to delete the original file");
        }
    }

    static class ComRowSet {
        ComRowSet() {
        }
    }
}
